<?php

namespace App\Http\Controllers\Backend;
use App\Common\ResponseCode;
use App\Http\Controllers\BaseController;
use App\Http\Middleware\AdminAuth;
use App\Params\Backend\GoodsListParams;
use App\Params\Backend\GoodsUpdateParam;
use App\Services\Goods\GoodsService;
use App\Traits\VerifyRequestParam;

/**
 * 商品控制器
 *
 * @package App\Http\Controllers\H5
 * @author WangBin
 * @date 2021-06/11
 */
class GoodsController extends BaseController
{
    use VerifyRequestParam;

    public function __construct()
    {
        $this->middleware(AdminAuth::class);
    }

    /**
     * 商品列表
     *
     * @return \Illuminate\Http\JsonResponse
     * @throws
     */
    public function getList()
    {
        $params = GoodsListParams::create();
        $goodsList = GoodsService::getInstance()->getList($params);
        return $this->success($goodsList);
    }

    /**
     * 获取货品详细信息
     *
     * @return GoodsController|\Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\BusinessException
     */
    public function get()
    {
        $ret = GoodsService::getInstance()->get($this->verifyInteger('id'));
        return $this->success($ret);
    }

    /**
     * 获取保存商品的初始化数据
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function init()
    {
        $ret = GoodsService::getInstance()->getInitData();
        return $this->success($ret);
    }

    /**
     * 更新商品信息
     *
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\BusinessException
     */
    public function update()
    {
        $param = GoodsUpdateParam::create();
        // 保存商品
        $opResult = GoodsService::getInstance()->update($param);
        if($opResult->getCode()) {
            return $this->success(['goods_id' => $opResult->getData()]);
        }
        return $this->fail(ResponseCode::GOODS_SAVE_FAIL, ['error' => $opResult->getMessage()]);
    }

    /**
     * 获取发布商品所需的初始数据
     *
     * @return mixed
     */
    public function getInit()
    {
        $ret = GoodsService::getInstance()->getInitData();
        return $this->success($ret);
    }
}
